import axios from '@/utils/axios'
import qs from 'qs'
import StringUtils from '@/utils/error/StringUtils'

export function getRequest(method, url, params, responseType) {
  if (!StringUtils.hasLength(responseType)) {
    responseType = ''
  }
  params = qs.stringify(params, { arrayFormat: 'repeat' })
  return axios({
    url: `${url}?${params}`,
    method: method,
    responseType
  })
}

export function formDataRequest(method, url, params, responseType) {
  if (!StringUtils.hasLength(responseType)) {
    responseType = ''
  }
  return axios({
    url: url,
    async: true,
    crossDomain: true,
    processData: false,
    contentType: false,
    method: method,
    responseType,
    headers: {
      'Content-Type': 'multipart/form-data;charset=UTF-8;'
    },
    transformRequest: [function() {
      return params
    }],
    data: params
  })
}

export function jsonRequest(method, url, data, responseType) {
  if (!StringUtils.hasLength(responseType)) {
    responseType = ''
  }
  return axios({
    url: url,
    method: method,
    data,
    responseType
  })
}

export function restfulRequest(method, url, responseType) {
  if (!StringUtils.hasLength(responseType)) {
    responseType = ''
  }
  return axios({
    url: url,
    method: method,
    responseType
  })
}
